import { AddOrUpdateContractQueryType } from "../service/contract";

// Mock数据
const mockContractList: any[] = [
  {
    id: "1",
    contractName: "合同1",
    supplierName: "供应商1",
    deviceId: "设备1",
    startDate: "2021-01-01",
    endDate: "2021-01-01",
  },
  {
    id: "2",
    contractName: "合同2",
    supplierName: "供应商2",
    deviceId: "设备2",
    startDate: "2021-01-01",
    endDate: "2021-01-01",
  },
  {
    id: "3",
    contractName: "合同3",
    supplierName: "供应商3",
    deviceId: "设备3",
    startDate: "2021-01-01",
    endDate: "2021-01-01",
  },
  {
    id: "4",
    contractName: "合同4",
    supplierName: "供应商4",
    deviceId: "设备4",
    startDate: "2021-01-01",
    endDate: "2021-01-01",
  },
  {
    id: "5",
    contractName: "合同5",
    supplierName: "供应商5",
    deviceId: "设备5",
    startDate: "2021-01-01",
    endDate: "2021-01-01",
  },
];

// 生成唯一ID
const generateId = (): string => {
  return Date.now().toString() + Math.random().toString(36).substr(2, 9);
};

// 模拟延迟
const delay = (ms = 300): Promise<void> => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

// 获取合同列表的mock
export const mockGetContractList = async (): Promise<{
  status: number;
  data: any[];
  message: string;
}> => {
  await delay();

  return {
    status: 0,
    data: mockContractList,
    message: "获取合同列表成功",
  };
};

// 添加或更新合同的mock
export const mockAddOrUpdateContract = async (
  params: AddOrUpdateContractQueryType
): Promise<{ status: number; data: any | null; message: string }> => {
  await delay();

  try {
    // 编辑合同
    if (params.id) {
      const index = mockContractList.findIndex(
        (contract) => contract.id === params.id
      );

      if (index === -1) {
        return {
          status: 1,
          data: null,
          message: "合同不存在",
        };
      }

      // 更新合同信息
      const updatedContract = {
        ...mockContractList[index],
        ...Object.fromEntries(
          Object.entries(params).filter(
            ([_, value]) => value !== null && value !== undefined
          )
        ),
      } as any;

      mockContractList[index] = updatedContract;

      return {
        status: 0,
        data: updatedContract,
        message: "更新合同成功",
      };
    }
    // 添加新合同
    else {
      // 验证必填字段
      if (!params.contractName || !params.startDate || !params.endDate) {
        return {
          status: 1,
          data: null,
          message: "合同名称和合同开始日期和合同结束日期为必填项",
        };
      }

      // 检查合同名称是否已存在
      const existingContract = mockContractList.find(
        (contract) => contract.contractName === params.contractName
      );
      if (existingContract) {
        return {
          status: 1,
          data: null,
          message: "该合同名称已存在",
        };
      }

      const newContract = {
        id: generateId(),
        contractName: params.contractName || "",
        supplierName: params.supplierName || "",
        deviceId: params.deviceId || "",
        startDate: params.startDate || 0,
        endDate: params.endDate || 0,
      };

      mockContractList.unshift(newContract); // 添加到列表开头

      return {
        status: 0,
        data: newContract,
        message: "添加合同成功",
      };
    }
  } catch (error) {
    return {
      status: 1,
      data: null,
      message: "操作失败，请重试",
    };
  }
};

// 删除合同的mock（可选，如果需要的话）
export const mockDeleteContract = async (
  id: string
): Promise<{ status: number; data: null; message: string }> => {
  await delay();

  const index = mockContractList.findIndex((contract) => contract.id === id);

  if (index === -1) {
    return {
      status: 1,
      data: null,
      message: "合同不存在",
    };
  }

  mockContractList.splice(index, 1);

  return {
    status: 0,
    data: null,
    message: "删除合同成功",
  };
};

// 根据ID获取单个合同的mock（可选）
export const mockGetContractById = async (
  id: string
): Promise<{ status: number; data: any | null; message: string }> => {
  await delay();

  const contract = mockContractList.find((contract) => contract.id === id);

  if (!contract) {
    return {
      status: 1,
      data: null,
      message: "合同不存在",
    };
  }

  return {
    status: 0,
    data: contract,
    message: "获取合同详情成功",
  };
};
